package com.itheima.day16.reflect.test2;

import java.lang.reflect.Constructor;

//        Class类获取构造方法对象的方法
public class Test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        // method1();
        // method2();
        // method3();
        // method4();

    }

    private static void method4() throws ClassNotFoundException, NoSuchMethodException {
        //        Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)	返回单个构造方法对象
        // 1.获取Class类对象
        Class<?> className = Class.forName("com.itheima.day16.reflect.test2.Student");
        Constructor<?> declaredConstructor1 = className.getDeclaredConstructor(String.class);
        System.out.println(declaredConstructor1);

        Constructor<?> declaredConstructor2 = className.getDeclaredConstructor();
        System.out.println(declaredConstructor2);

        Constructor<?> declaredConstructor3 = className.getDeclaredConstructor(String.class, int.class);
        System.out.println(declaredConstructor3);
    }

    private static void method3() throws NoSuchMethodException {
        //        Constructor<T> getConstructor(Class<?>... parameterTypes)	返回单个公共构造方法对象
        // 1.获取Class类对象
        Student s = new Student();
        Class className = s.getClass();
        Constructor constructor1 = className.getConstructor();
        System.out.println(constructor1);
        Constructor constructor2 = className.getConstructor(String.class, int.class);
        System.out.println(constructor2);
    }

    private static void method2() {
        //        Constructor<?>[] getDeclaredConstructors()	返回所有构造方法对象的数组
        // 1.获取Class类对象
        Class className = Student.class;
        Constructor[] declaredConstructors = className.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
    }

    private static void method1() throws ClassNotFoundException {
        //        Constructor<?>[] getConstructors()      	返回所有公共构造方法对象的数组
        // 1. 获取Class对象
        Class className = Class.forName("com.itheima.day16.reflect.test2.Student");
        Constructor[] constructors = className.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
    }
}
